rm(list = ls())

## packages 

library(tidyverse)
library(fixest)

## Function to discretize 

make_binary <- function (v, method = "median", q = 0.75) 
{
  if (!(method %in% c("mean", "median", "quantile"))) {
    stop("Unknown method")
  }
  if (!method == "quantile") {
    cutoff = ifelse(method == "mean", mean(v, na.rm = T), 
                    median(v, na.rm = T))
    b <- ifelse(v > cutoff, 1, 0)
  }
  else {
    cutoff = quantile(v, probs = q, na.rm = T)
    b <- ifelse(v > cutoff, 1, 0)
  }
  b
}

## Load data 

dat <- readRDS('data/civey_clean.rds')  
## 

mod_cutoff <- seq(0.1, .9, .1)

res <- lapply(mod_cutoff, function(c){
  
  dat <- dat %>%
    mutate(mod_var = make_binary(foreign_non_eu_share,
                                 method = 'quantile',
                                 q = c))
  ## 2WFE 
  
  m2wfe <- feols(vote_afd ~  treated_post| user_id + date, 
                 data = dat %>% filter(mod_var == 1),
                 cluster = ~ user_id) %>%
    broom::tidy(conf.int = T) %>% 
    mutate(mod_cutoff = c,
           conf.low90 = estimate - 1.645 * std.error,
           conf.high90 = estimate + 1.645 * std.error)
  
  return(m2wfe)
  
}) %>%
  reduce(bind_rows) %>%
  filter(term == 'treated_post')

## 

p1 <- ggplot(res, aes(x = mod_cutoff, y = estimate, ymin = conf.low, ymax = conf.high)) + 
  geom_errorbar(width = 0, aes(ymin = conf.low, ymax = conf.high)) + 
  geom_errorbar(width = 0, linewidth = .8, aes(ymin = conf.low90, ymax = conf.high90)) + 
  geom_point(shape = 21, fill = 'white') + 
  theme_minimal() + 
  geom_hline(yintercept = 0) + 
  labs(x = 'Immigrant Share quantile',
       y = 'Effect on AfD\nsupport (in p.p.)') +
  scale_x_continuous(breaks = seq(0.1, 0.9, .1))

p1 


